package com.lazydong.apiforward.server.controller;

import com.lazydong.apiforward.gateway.model.RouteDefinitionInfo;
import com.lazydong.apiforward.server.component.DynamicRouteComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/route")
@Order(Ordered.LOWEST_PRECEDENCE)
public class RouteController implements CommandLineRunner {
    @Autowired
    private DynamicRouteComponent dynamicRouteComponent;
    @Override
    public void run(String... args) throws Exception {
        RouteDefinitionInfo info1 = new RouteDefinitionInfo();
        info1.setServiceId("baidu");
        info1.setPredicate("/www_baidu_com/**");
        info1.setUrl("https://www.baidu.com");
        addRoute(info1);
        RouteDefinitionInfo info2 = new RouteDefinitionInfo();
        info2.setServiceId("tencent");
        info2.setPredicate("/www_tencent_com/**");
        info2.setUrl("https://www.tencent.com");
        addRoute(info2);
    }

    @PostMapping("/addRoute")
    public boolean addRoute(RouteDefinitionInfo info) {
        dynamicRouteComponent.routeChangeEvent(info);
        return false;
    }

    @PostMapping("/updateRoute")
    public boolean updateRoute(RouteDefinitionInfo info) {

        return false;
    }

    @PostMapping("/deleteRoute")
    public boolean deleteRoute(RouteDefinitionInfo info) {

        return false;
    }
}
